package com.test.service.impl;

import com.test.dao.*;
import com.test.model.BuildMeterCurrentData;
import com.test.model.HeatBase;
import com.test.model.HeatingSeason;
import com.test.model.HouseMeterCurrentData;
import com.test.service.IHeatingSeasonService;
import com.test.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ZZF on 2017/8/9.
 */
@Service
@Transactional
public class HeatingSeasonServiceImpl implements IHeatingSeasonService {

    @Autowired
    IHeatingSeasonDao heatingSeasonDao;
    @Autowired
    IHeatBaseDao heatBaseDao;
    @Autowired
    IHouseMeterDao houseMeterDao;
    @Autowired
    IHouseMeterCurrentDao houseMeterCurrentDao;
    @Autowired
    IBuildMeterCurrentDao buildMeterCurrentDao;


    //改变底数表中的第一次底数数据的baseDate 和 数据

    @Override
    public void updateHeatingSeason( ) {

        //获取当前采暖季的时间
        HeatingSeason current = heatingSeasonDao.findCurrent();

        //当前采暖季的年份
        String currentYear = current.getYears();

        //采暖季结束时间
        Date endDate = current.getEndDate();
        //采暖季底数时间
        Date baseDate = current.getBaseDate();
        //当前时间按
        Date now = new Date();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String format = df.format(now);// new Date()为获取当前系统时间

        //截取时间年份
        String nowYear = format.substring(0, 4);
        //截取日期
        String subNowDate = format.substring(0, 16);

        String tempDate = df.format(baseDate);
        //截取日期
        String subTempDate = tempDate.substring(0, 16);



        //设置一个标志，表示是否是底数数据
        boolean isStart = false;

        //先比较年份
        if (nowYear.equals(currentYear)) {

            //在比较日期，大于或者等于底数时间，小于结束时间的才能进行底数赋值
            if (subNowDate.equals(subTempDate)) {

                isStart = true;


            } else if (now.getTime() > baseDate.getTime() && now.getTime() < endDate.getTime()) {

                isStart = true;

            }




        }


        if (isStart) {

            //  先把户表的底数数据全取出来，
            List<HeatBase> houseMeterBase = heatBaseDao.findHouseMeter();

            //取的楼表的底数数据
            List<HeatBase> buildMeterBase = heatBaseDao.findBuildMeter();

            //遍历底数，用底数的id比对户表实时里面的数据

            if (!houseMeterBase.isEmpty()) {
                for (HeatBase heatBase : houseMeterBase) {

                    HouseMeterCurrentData houseMeterCurrentData = houseMeterCurrentDao.
                            get(HouseMeterCurrentData.class, heatBase.getEquId());

                    //把实时表里的数据信息填充到底数表中
                    if (houseMeterCurrentData != null) {

                        BigDecimal accHeat = houseMeterCurrentData.getAccHeat();
                        BigDecimal accFlow = houseMeterCurrentData.getAccFlow();

                        heatBase.setBaseDate(new Date());
                        heatBase.setBeginBaseCollectType("系统抄表");
                        heatBase.setBaseFlow(accFlow);
                        heatBase.setBaseHeat(accHeat);
                        heatBase.setEndDate(new Date());
                        heatBase.setBeginEndCollectType("系统抄表");
                        heatBase.setEndHeat(accHeat);
                        heatBase.setEndFlow(accFlow);

                        BigDecimal zero = new BigDecimal(0);

                        heatBase.setSeasonFlow(zero);
                        heatBase.setSeasonHeat(zero);


                        heatBaseDao.update(heatBase);
                    }
                }
            }

            if (!buildMeterBase.isEmpty()) {
                for (HeatBase heatBase : buildMeterBase) {

                    BuildMeterCurrentData buildMeterCurrentData = buildMeterCurrentDao.
                            get(BuildMeterCurrentData.class, heatBase.getEquId());

                    //把实时表里的数据信息填充到底数表中
                    if (buildMeterCurrentData != null) {

                        BigDecimal accFlow = buildMeterCurrentData.getAccFlow();
                        BigDecimal accHeat = buildMeterCurrentData.getAccHeat();

                        heatBase.setBaseDate(new Date());
                        heatBase.setBeginBaseCollectType("系统抄表");
                        heatBase.setBaseFlow(accFlow);
                        heatBase.setBaseHeat(accHeat);
                        heatBase.setEndDate(new Date());
                        heatBase.setBeginEndCollectType("系统抄表");
                        heatBase.setEndHeat(accHeat);
                        heatBase.setEndFlow(accFlow);

                        BigDecimal zero = new BigDecimal(0);

                        heatBase.setSeasonFlow(zero);
                        heatBase.setSeasonHeat(zero);

                        heatBaseDao.update(heatBase);
                    }

                }
            }


        }


    }




}
